import openpyxl
import os


def write_xlsx(filename, sheet_index, sheet_name, data):
    """
    Judge whether the file exists,
    if it exists, open the original file,
    otherwise create a new file
    :param filename: Target file path
    :param sheet_index: create a new sheet's index
    :param sheet_name: create a new sheet's name
    :param data: add data
    """
    if os.path.exists(filename):
        "the file has already exists"
        wb = openpyxl.load_workbook(filename)
    else:
        "not exists"
        wb = openpyxl.Workbook()
    sheet = wb.create_sheet(title=sheet_name, index=sheet_index)

    for item in data:
        sheet.append(item)

    wb.save(filename)


def Read_xlsx(filename, sheet_index):
    wb = openpyxl.load_workbook(filename)
    names = wb.sheetnames  # 获取所有工作表名
    sheet = wb[names[sheet_index]]  # wb.get_sheet_by_name(name) 已经废弃,使用wb[name] 获取指定工作表
    maxRow = sheet.max_row  # 获取最大行数
    maxColumn = sheet.max_column  # 获取最大列数
    data = []
    for i in range(maxRow):
        row_data = []
        for j in range(maxColumn):
            row_data.append(sheet.cell(row=i + 1, column=j + 1).value)
        data.append(row_data)
    return data


def Read_xlsx_a_row(filename, sheet_index, row_num):
    wb = openpyxl.load_workbook(filename)
    names = wb.sheetnames  # 获取所有工作表名
    sheet = wb[names[sheet_index]]  # wb.get_sheet_by_name(name) 已经废弃,使用wb[name] 获取指定工作表
    maxColumn = sheet.max_column  # 获取最大列数
    data = []

    for j in range(maxColumn):
        data.append(sheet.cell(row=row_num + 1, column=j + 1).value)

    return data


if __name__ == '__main__':
    print('0')
    # filename = 'C:/Users/LENOVO/Desktop/UI_Project/test.xlsx'
    # sheet_index = 0
    # sheet_name = "test_sheet1"
    # data = [[1, "小明"], [2, "小红"]]
    # write_xlsx(filename, sheet_index, sheet_name, data)

    # filename = 'C:\\Users\\LENOVO\\Desktop\\UI_Project\\datastation.xlsx'
    # data = Read_xlsx(filename, 0)
